package com.ruoyi.shopping.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.dto.MallProductAddOrUpdateDto;
import com.ruoyi.dto.ProductParams;
import com.ruoyi.shopping.domain.MallSkus;
import com.ruoyi.shopping.mapper.MallSkusMapper;
import com.ruoyi.vo.MallProductVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.shopping.domain.MallProduct;
import com.ruoyi.shopping.service.IMallProductService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品管理Controller
 *
 * @author cws
 * @date 2024-11-19
 */
@RestController
@RequestMapping("/shopping/product")
public class MallProductController extends BaseController {
    @Autowired
    private IMallProductService mallProductService;

    @Autowired
    private MallSkusMapper mallSkusMapper;


    /**
     * 查询商品管理列表
     */
    @PreAuthorize("@ss.hasPermi('shopping:product:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProductParams params) {
        startPage();
//        List<MallProduct> list = mallProductService.selectMallProductList(mallProduct);
        List<MallProductVo> list = mallProductService.selectMallProductListVO(params);
        return getDataTable(list);
    }

    @GetMapping("/commonList")
    public TableDataInfo commonList(ProductParams params) {
        startPage();
//        List<MallProduct> list = mallProductService.selectMallProductList(mallProduct);
        List<MallProductVo> list = mallProductService.selectMallProductListVO(params);
        return getDataTable(list);
    }

    /**
     * 导出商品管理列表
     */
    @PreAuthorize("@ss.hasPermi('shopping:product:export')")
    @Log(title = "商品管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MallProduct mallProduct) {
        List<MallProduct> list = mallProductService.selectMallProductList(mallProduct);
        ExcelUtil<MallProduct> util = new ExcelUtil<MallProduct>(MallProduct.class);
        util.exportExcel(response, list, "商品管理数据");
    }

    /**
     * 获取商品管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('shopping:product:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(mallProductService.selectMallProductById(id));
    }

    /**
     * 新增商品管理
     */
    @PreAuthorize("@ss.hasPermi('shopping:product:add')")
    @Log(title = "商品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MallProductAddOrUpdateDto mallProductAddOrUpdateDto) {
        return toAjax(mallProductService.SaveMallProduct(mallProductAddOrUpdateDto));
    }

    /**
     * 修改商品管理
     */
    @PreAuthorize("@ss.hasPermi('shopping:product:edit')")
    @Log(title = "商品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MallProductAddOrUpdateDto mallProductAddOrUpdateDto) {
        return toAjax(mallProductService.updateMallProduct(mallProductAddOrUpdateDto));
    }


    @PreAuthorize("@ss.hasPermi('shopping:product:verifyStatus')")
    @Log(title = "商品审核状态管理", businessType = BusinessType.UPDATE)
    @PutMapping("/verifyStatus/{id}")
    public AjaxResult updateVerifyStatus(@PathVariable("id") Long id) {
        return toAjax(mallProductService.updateVerifyStatus(id));
    }

    /**
     * 删除商品管理
     */
    @PreAuthorize("@ss.hasPermi('shopping:product:remove')")
    @Log(title = "商品管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(mallProductService.deleteMallProductByIds(ids));
    }

    /**
     * 查看sku
     */
    @PreAuthorize("@ss.hasPermi('shopping:product:showSku')")
    @GetMapping("/sku/{productId}")
    public AjaxResult sku(@PathVariable("productId") Long productId) {
        return success(mallSkusMapper.selectMallSkusOfPro(productId));
    }


    /**
     * 修改sku
     */
    @PreAuthorize("@ss.hasPermi('shopping:product:editSku')")
    @Log(title = "商品Sku修改", businessType = BusinessType.UPDATE)
    @PutMapping("/updateSku")
    public AjaxResult updateSku(@RequestBody List<MallSkus> mallSkusList) {
        return toAjax(mallProductService.updateSku(mallSkusList));
    }


    /**
     * 查询商品名称
     */
    @GetMapping("/productName")
    public AjaxResult productName(@RequestParam("name") String name) {
        return success(mallProductService.SelectProductName(name));
    }


}
